package com.scap.authen.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.support.RequestContextUtils;

import com.scap.authen.entity.*;
import com.scap.authen.service.SiteService;
import com.scap.authen.service.UserLoginService;
import com.scap.authen.service.UserMappingService;
import com.scap.authen.util.DataTable;

@Controller
@RequestMapping(value = "/user")
public class UserManagementController {

	@Autowired
	private UserLoginService voUserLoginService;
	@Autowired
	private UserMappingService voUserMappingService;
	@Autowired
	private SiteService voSiteService;
	@Autowired
	private ApplicationContext context;
	DataTable voDataTable = new DataTable();

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView application(HttpServletRequest request) {
		UserLogin voUserLogin = new UserLogin();
		String vaUserMapping = "";
		String vaUserId = request.getParameter("vaUserId");
		String vaProcessType = "";
		if(vaUserId == null || vaUserId.equals("")){
			vaProcessType = "insert";
		}else{
			voUserLogin = voUserLoginService.getUserLoginByUserId(vaUserId);
			vaUserMapping = voUserMappingService.getUserMappingByUserId(vaUserId);
			vaProcessType = "update";
		}
		ModelAndView voModelAndView = new ModelAndView("login/userManagement","userLogin",voUserLogin);
		voModelAndView.addObject("page", "User Management");
		voModelAndView.addObject("processType", vaProcessType);
		voModelAndView.addObject("vaUserMapping", vaUserMapping);
		voModelAndView.addObject("oLanguage", voDataTable.getDataTableLanguage(context,request));
		return voModelAndView;
	}

	@RequestMapping(value = "/siteRoleDataTables", method = RequestMethod.GET, 
			produces = "application/json; charset=utf-8")
	public @ResponseBody String springPaginationDataTables(
			HttpServletRequest request) throws IOException {

		List<HashMap<String, String>> voSiteAndRoleListHashMap = voSiteService.getAllSiteAndRole();
		return voDataTable.springPaginationDataTables(request,voSiteAndRoleListHashMap);
	}
	
	@ModelAttribute("vaStatus")
	public Map<String,String> statusList() {
		
		//Data referencing for java skills list box
		Map<String,String> voStatus = new LinkedHashMap<String,String>();
		voStatus.put("A", "Active");
		voStatus.put("I", "Inactive");
		
		return voStatus;
	}

	@RequestMapping(method = RequestMethod.POST)
	public ModelAndView manageUserLogin(@RequestParam("vaSiteAndRoleId") String vaSiteAndRoleId,
			@RequestParam("processType") String vaProcessType,
			@RequestParam("vaUserIdTemp") String vaUserIdTemp,
			@Valid UserLogin userLogin,
			BindingResult result,
			HttpServletRequest request) {
		
		String vaMessage = "";
		String vaStatus = "";
		String vaUrl = "";
		Locale voLocale = RequestContextUtils.getLocale(request);
		
		if(vaProcessType.equals("insert")){
			vaStatus = voUserLoginService.insertUserLogin(userLogin,vaSiteAndRoleId, result, request);
			vaUrl = "login/userManagement";
		}else if(vaProcessType.equals("update")){
			userLogin.setVaUserId(vaUserIdTemp);
			vaStatus = voUserLoginService.updateUserLogin(userLogin,vaSiteAndRoleId, result, request);
			vaUrl = "redirect:user/edit";
		}
		
		if(vaStatus.equals("success")){
			if(vaProcessType.equals("insert") || vaProcessType.equals("update")){
				vaMessage = context.getMessage("alert.save.success", null, voLocale);
			}else{
				vaMessage = context.getMessage("alert.delete.success", null, voLocale);
			}
		}else{
			if(vaProcessType.equals("insert") || vaProcessType.equals("update")){
				vaMessage = context.getMessage("alert.save.fail", null, voLocale);
			}else{
				vaMessage = context.getMessage("alert.delete.fail", null, voLocale);
			}
		}
		
		ModelAndView voModelAndView = new ModelAndView(vaUrl);
		voModelAndView.addObject("message", vaMessage!=null?vaMessage.replace("_Name_", context.getMessage("class.UserLogin", null, voLocale)):"");
		voModelAndView.addObject("status", vaStatus);
		
		return voModelAndView;
	}

}
